<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="renderer" content="webkit" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>千锋教育大前端React教学</title>
  <!-- 引入react -->
  <script src="../../lib/js/react.js"></script>
  <!-- 引入react-dom -->
  <script src="../../lib/js/react-dom.js"></script>
  <!-- 引入babel 用来解析jsx -->
  <script src="../../lib/js/babel.js"></script>
</head>

<body style="margin:100px;font-size:22px;">
  <!-- 定义一个容器 -->
  <div id="app"></div>
  <script type="text/babel"> /*此处一定要写babel*/
    class Parent extends React.Component {
      constructor() {
        super();
        this.state = { name: '朱小明' };
        console.log('parent constructor');
      }
      static getDerivedStateFromProps(prevProps, prevState) {
        console.log('parent getDerivedStateFromProps', JSON.stringify({ prevProps, prevState }));
      }
      componentDidMount() {
        console.log('parent componentDidMount');
      }
      getSnapshotBeforeUpdate(prevProps, prevState) {
        console.log('parent getSnapshotBeforeUpdate', JSON.stringify({ prevProps, prevState }));
      }
      componentDidUpdate(prevProps, prevState, snapshot) {
        console.log('parent componentDidUpdate', JSON.stringify({ prevProps, prevState }));
      }
      componentWillUnmount() {
        console.log('parent componentWillUnmount');
      }
      render() {
        const { name } = this.state;
        return (
          <div>
            <div>我是第一代</div>
            <button onClick={()=>{ this.setState({name: name === '朱小明' ? '朱大肠' : '朱小明'}) }}>重新取个名字</button>
            <hr />
            <Child name={name}></Child>
          </div>
        )
      }
    }
    class Child extends React.Component {
      constructor() {
        super();
        this.state = { age: 0 };
        console.log('--child constructor');
      }
      static getDerivedStateFromProps(prevProps, prevState) {
        console.log('--child getDerivedStateFromProps', JSON.stringify({ prevProps, prevState }));
      }
      componentDidMount() {
        console.log('--child componentDidMount');
      }
      getSnapshotBeforeUpdate(prevProps, prevState) {
        console.log('--child getSnapshotBeforeUpdate', JSON.stringify({ prevProps, prevState }));
      }
      componentDidUpdate(prevProps, prevState, snapshot) {
        console.log('--child componentDidUpdate', JSON.stringify({ prevProps, prevState }));
      }
      componentWillUnmount() {
        console.log('--child componentWillUnmount');
      }
      render() {
        const { name } = this.props;
        const { age } = this.state;
        return (
          <div>
            <button onClick={()=>{ this.setState({ age: age+1 }) }}>过了一年</button>
            <div>我是第二代，我叫{name}，我{age}岁了</div>
          </div>
        )
      }
    }
    // 使用js函数返回一个组件
    function App() {
      return (
        <Parent />
      );
    }
    // 挂载应用
    ReactDOM.createRoot(document.getElementById('app')).render(<App />);
  </script>
</body>

</html>